//
//  MGUserInfoManager.m
//  MortgageHelper
//
//  Created by zhangxiang on 16/8/8.
//  Copyright © 2016年 zhangxiang. All rights reserved.
//

#import "MGUserInfoManager.h"

#import "ZXBaseNavigationViewController.h"

#import "JPUSHService.h"



static MGUserInfoManager* _instance = nil;

#define ArchivingUserInfoDataKey @"userInfos"

@implementation MGUserInfoManager

+ (instancetype) sharedInstance {
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:NULL] init] ;
                [_instance unarchiverModel];
        
    });
    return _instance ;
}

+ (id) allocWithZone:(struct _NSZone *)zone {
    return [MGUserInfoManager sharedInstance];
}

- (id)copyWithZone:(struct _NSZone *)zone {
    return [MGUserInfoManager sharedInstance];
}

-(void)setUserInfo:(MGUserInfoModel *)userInfo{
     _userInfo = userInfo;
    //将对象保存
        NSString *filePath = [DocumentDir stringByAppendingPathComponent:ArchivingUserInfoDataKey];
        NSMutableData *data = [[NSMutableData alloc] init];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        [archiver encodeObject:userInfo forKey:ArchivingUserInfoDataKey];
        [archiver finishEncoding];
        [data writeToFile:filePath atomically:YES];
        
        NSLog(@"------保存-----%@--- %@---",_userInfo,userInfo);
//       if (_userInfo.userId) {
//           [JPUSHService setAlias:[NSString stringWithFormat:@"%@", _userInfo.userId] callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
//       }
    
    NSLog(@"token %@ %@",_userInfo.token,userInfo.token);
}

- (void)tagsAliasCallback:(int)iResCode
                     tags:(NSSet*)tags
                    alias:(NSString*)alias
{
    NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags , alias);
}

-(void)setUserTypeModelList:(NSArray *)userTypeModelList{
    
    NSString *filePath = [DocumentDir stringByAppendingPathComponent:@"userTypeModelList"];
    
    [NSKeyedArchiver archiveRootObject:userTypeModelList toFile:filePath];

    _userTypeModelList=userTypeModelList;
    
}

/**
 *  从文件中取出数据
 */

- (void)unarchiverModel {
    
    NSString *filePath = [DocumentDir stringByAppendingPathComponent:ArchivingUserInfoDataKey];
    NSData *data = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    MGUserInfoModel *userInfo = [unarchiver decodeObjectForKey:ArchivingUserInfoDataKey];
    _userInfo = userInfo;
    
//    if (_userInfo.userId) {
//    [JPUSHService setAlias:[NSString stringWithFormat:@"%@", _userInfo.userId] callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
//    }
    
    NSLog(@"------取出用户信息缓存-----%@---",_userInfo);
    [unarchiver finishDecoding];
    
//    NSString *userTypeModelListPath = [DocumentDir stringByAppendingPathComponent:@"userTypeModelList"];
//    NSArray *newPersonsArr = [NSKeyedUnarchiver unarchiveObjectWithFile:userTypeModelListPath];
//
//    _userTypeModelList=newPersonsArr;
}


-(void)determineWhetherTheLogin{
    
    if (!UserInfoManage.userInfo.userId) {
        [self logOut];
    }else{
        
    }
}


- (void)logOut{
    [[NSNotificationCenter defaultCenter] postNotificationName:MG_USER_LOGIN_OUT object:nil];
}

@end
